﻿using Autofac.Core;
using Microsoft.AspNetCore.Mvc;
using Org.BouncyCastle.Crypto;
using System;
using System.Threading.Tasks;
using WaterCloud.Code;
using WaterCloud.Domain.ContentManage;
using WaterCloud.Domain.Entity.STZBManage;
using WaterCloud.Service.STZBManage;
using WaterCloud.Service.SystemManage;

namespace WaterCloud.Web.Areas.STZBManage.Controllers
{
    /// <summary>
    /// 率土之滨用户管理
    /// </summary>
    [Area("STZBManage")]
    public class UserController : BaseController
    {
        /// <summary>
        /// 属性注入示例
        /// </summary>
        public STZBService _userService { get; set; }

        public STZB_AccountService _accountService { get; set; }

        public STZB_CbgUrlService _cbgUrlService { get; set; }

        public WeaponsService _weaponsService { get; set; }

        public SsqhService _ssqhService { get; set; }

        public WeaponsInfo_Service weaponsInfo_Service { get; set; }

        #region 视图

        /// <summary>
        /// 用户管理视图
        /// </summary>
        /// <returns></returns>
        public IActionResult UserIndex()
        {
            return View();
        }

        /// <summary>
        /// 账号管理视图
        /// </summary>
        /// <returns></returns>
        public IActionResult AccountIndex()
        {
            return View();
        }

        /// <summary>
        /// 藏宝阁链接视图
        /// </summary>
        /// <returns></returns>
        public IActionResult CBGUrl_Index()
        {
            return View();
        }

        /// <summary>
        /// 武器视图
        /// </summary>
        /// <returns></returns>
        public IActionResult Weapons_Index()
        {
            return View();
        }

        /// <summary>
        /// 试师区号视图
        /// </summary>
        /// <returns></returns>
        public IActionResult Ssqh_Index()
        {
            return View();
        }

        /// <summary>
        /// 武器详情视图
        /// </summary>
        /// <returns></returns>
        public IActionResult Weapons_Info()
        {
            return View();
        }

        #endregion


        #region 接口

        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="keyword">关键词</param>
        /// <param name="stime">注册开始日期</param>
        /// <param name="etime">注册结束日期</param>
        /// <returns></returns>
        [HandlerAjaxOnly] //ajax校验
        public async Task<ActionResult> GetGridJson(SoulPage<User> pagination, string keyword, string stime, string etime)
        {
            if (string.IsNullOrEmpty(pagination.field))
            {
                pagination.field = "CreateTime";
                pagination.order = "desc";
            }
            var data = await _userService.GetGridJson(pagination, keyword, stime, etime);
            return Content(pagination.setData(data).ToJson());
        }

        /// <summary>
        /// 获取账号列表
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="keyword">关键词</param>
        /// <param name="sell_state">出售状态 1:公示期、2:可购买</param>
        /// <param name="read_State">读取状态</param>
        /// <param name="state_val">账号状态</param>
        /// <param name="timeStr">公告日期范围</param>
        /// <param name="createTime">注册日期范围</param>
        /// <param name="userid">当前登入用户id</param>
        /// <param name="monthNum">月数 默认最近一个月</param>
        /// <returns></returns>
        [HandlerAjaxOnly] //ajax校验
        public async Task<ActionResult> Get_Account_GridJson(SoulPage<Account> pagination, string keyword, int sell_state, string read_State, int state_val, string timeStr, string createTime, string userid, int monthNum = 1)
        {
            if (string.IsNullOrEmpty(pagination.field))
            {
                pagination.field = "CreateTime";
                pagination.order = "desc";
            }
            var data = await _accountService.Get_Account_GridJson(pagination, keyword, sell_state, read_State, state_val, timeStr, createTime, monthNum, userid);
            return Content(pagination.setData(data).ToJson());
        }

        /// <summary>
        /// 删除用户数据
        /// </summary>
        /// <param name="id">数据id,多个使用逗号隔开</param>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly] //ajax校验
        [HandlerAuthorize] //权限校验
        public async Task<ActionResult> DeleteForm(string id)
        {
            try
            {
                await _userService.DeleteForm(id);
                return await Success("操作成功。", "", id, DbLogType.Delete);
            }
            catch (Exception ex)
            {
                return await Error(ex.Message, "", id, DbLogType.Delete);
            }
        }


        /// <summary>
        /// 删除账号数据
        /// </summary>
        /// <param name="ids">数据id，多个值使用逗号隔开</param>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly] //ajax校验
        [HandlerAuthorize] //权限校验
        public async Task<ActionResult> Delete_Account(string ids)
        {
            try
            {
                await _accountService.Delete_Account(ids);
                return await Success("操作成功。", "", ids, DbLogType.Delete);
            }
            catch (Exception ex)
            {
                return await Error(ex.Message, "", ids, DbLogType.Delete);
            }
        }

        /// <summary>
        /// 获取藏宝阁链接列表
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="keyword">关键词</param>
        /// <param name="open_State">点开状态 0:未点开、1:点开</param>
        /// <returns></returns>
        [HandlerAjaxOnly] //ajax校验
        public async Task<ActionResult> Get_CBGUrl_GridJson(SoulPage<CbgUrl> pagination, string keyword, int open_State)
        {
            if (string.IsNullOrEmpty(pagination.field))
            {
                pagination.field = "CreateTime";
                pagination.order = "desc";
            }
            var data = await _cbgUrlService.Get_CBGUrl_GridJson(pagination, keyword, open_State);
            return Content(pagination.setData(data).ToJson());
        }

        /// <summary>
        /// 删除藏宝阁链接数据
        /// </summary>
        /// <param name="ids">数据id，多个值用逗号隔开</param>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly] //ajax校验
        [HandlerAuthorize] //权限校验
        public async Task<ActionResult> Delete_CBGUrl(string ids)
        {
            try
            {
                await _cbgUrlService.Delete_CBGUrl(ids);
                return await Success("操作成功。", "", ids, DbLogType.Delete);
            }
            catch (Exception ex)
            {
                return await Error(ex.Message, "", ids, DbLogType.Delete);
            }
        }

        /// <summary>
        /// 获取武器列表
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="keyword">关键词</param>
        /// <returns></returns>
        [HandlerAjaxOnly] //ajax校验
        public async Task<ActionResult> Get_Weapons_GridJson(SoulPage<Weapons> pagination, string keyword)
        {
            if (string.IsNullOrEmpty(pagination.field))
            {
                pagination.field = "CreateTime";
                pagination.order = "desc";
            }
            var data = await _weaponsService.Get_Weapons_GridJson(pagination, keyword);
            return Content(pagination.setData(data).ToJson());
        }

        /// <summary>
        /// 删除武器数据
        /// </summary>
        /// <param name="ids">数据id，多个值用逗号隔开</param>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly] //ajax校验
        [HandlerAuthorize] //权限校验
        public async Task<ActionResult> Delete_Weapons(string ids)
        {
            try
            {
                await _weaponsService.Delete_Weapons(ids);
                return await Success("操作成功。", "", ids, DbLogType.Delete);
            }
            catch (Exception ex)
            {
                return await Error(ex.Message, "", ids, DbLogType.Delete);
            }
        }


        /// <summary>
        /// 获取试师区号列表
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="keyword">关键词</param>
        /// <returns></returns>
        [HandlerAjaxOnly] //ajax校验
        public async Task<ActionResult> Get_Ssqh_GridJson(SoulPage<Ssqh> pagination, string keyword)
        {
            if (string.IsNullOrEmpty(pagination.field))
            {
                pagination.field = "CreateTime";
                pagination.order = "desc";
            }
            var data = await _ssqhService.Get_Ssqh_GridJson(pagination, keyword);
            return Content(pagination.setData(data).ToJson());
        }

        /// <summary>
        /// 删除试师区号数据
        /// </summary>
        /// <param name="ids">数据id，多个值用逗号隔开</param>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly] //ajax校验
        [HandlerAuthorize] //权限校验
        public async Task<ActionResult> Delete_Ssqh(string ids)
        {
            try
            {
                await _ssqhService.Delete_Ssqh(ids);
                return await Success("操作成功。", "", ids, DbLogType.Delete);
            }
            catch (Exception ex)
            {
                return await Error(ex.Message, "", ids, DbLogType.Delete);
            }
        }

        /// <summary>
        /// 获取武器详情列表
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="keyword">关键词</param>
        /// <param name="level">武器层级</param>
        /// <returns></returns>
        [HandlerAjaxOnly] //ajax校验
        public async Task<ActionResult> Get_WeaponsInfo_GridJson(SoulPage<WeaponsInfo> pagination, string keyword, int level)
        {
            if (string.IsNullOrEmpty(pagination.field))
            {
                pagination.field = "CreateTime";
                pagination.order = "desc";
            }
            var data = await weaponsInfo_Service.Get_WeaponsInfo_GridJson(pagination, keyword, level);
            return Content(pagination.setData(data).ToJson());
        }

        /// <summary>
        /// 删除武器详情数据
        /// </summary>
        /// <param name="ids">数据id</param>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly] //ajax校验
        [HandlerAuthorize] //权限校验
        public async Task<ActionResult> Delete_WeaponsInfo(int[] ids)
        {
            try
            {
                await weaponsInfo_Service.Delete_WeaponsInfo(ids);
                return await Success("操作成功。", "", ids, DbLogType.Delete);
            }
            catch (Exception ex)
            {
                return await Error(ex.Message, "", ids, DbLogType.Delete);
            }
        }

        /// <summary>
        /// 修改查看状态
        /// </summary>
        /// <param name="idStr">数据id</param>
        /// <param name="userid">当前查看用户id</param>
        /// <returns></returns>
        [HttpGet]
        [HandlerAjaxOnly]
        public async Task<ActionResult> Update_ReadState(string idStr,string userid)
        {
            try
            {
                await _accountService.Update_ReadState(idStr, userid);
                return await Success("操作成功。", "", "");
            }
            catch (Exception ex)
            {
                return await Error(ex.Message, "", "");
            }
        }

        /// <summary>
        /// 添加或取消收藏账号
        /// </summary>
        /// <param name="ids">账号id</param>
        /// <param name="type">1:收藏、2:取消收藏</param>
        /// <param name="userid">用户id</param>
        /// <returns></returns>
        public async Task<ActionResult> AddOrEdit_Collection(string[] ids, int type, string userid)
        {
            try
            {
                await _accountService.AddOrEdit_Collection(ids, type, userid);
                return await Success("操作成功。", "", "");
            }
            catch (Exception ex)
            {
                return await Error(ex.Message, "", "");
            }
        }

        #endregion
    }
}
